package com.zodiac.app.szse.d.d0602.d060201;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.lzm320a99981e.component.dataautomation.DataAutomationContext;
import com.github.lzm320a99981e.component.dataautomation.Extractors;
import com.github.lzm320a99981e.component.dataautomation.Processor;
import com.google.common.base.Preconditions;
import org.apache.commons.lang3.EnumUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Extractors(D060201Extractor.class)
@Component
public class D060201Processor implements Processor<List<Map>, List<D060201Model>> {
    private final Pattern REPORT_YEAR_PATTERN = Pattern.compile("[\\d]{4}");
    private final List<String> EXCLUDES = Arrays.asList("已取消", "英文版", "报告摘要", "报告正文");

    @Override
    public List<D060201Model> process(List<Map> request) {
        Preconditions.checkNotNull(request);

        final D060201Extractor.Request extractRequest = (D060201Extractor.Request) DataAutomationContext.getCurrentContext().getExtractRequest();
        final D060201Extractor.ReportCategory reportCategory = D060201Extractor.ReportCategory.of(extractRequest.getBigCategoryId().get(0));

        return request.stream().map(item -> {
            D060201Model model = convert(item);
            if (Objects.isNull(model.getReportCategory())) {
                model.setReportCategory(reportCategory.name());
            }
            return model;
        })
                // 排除非正式的报告
                .filter(item -> EXCLUDES.stream().noneMatch(item.getReportTitle()::contains))
                // 排除自定义条件的报告
                .filter(item -> extractRequest.getFilter().apply(item)).collect(Collectors.toList());
    }

    public D060201Model convert(Map data) {
        D060201Model model = new D060201Model();
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(data));
        model.setStockCode(jsonObject.getJSONArray("secCode").getString(0));
        model.setStockName(jsonObject.getJSONArray("secName").getString(0));
        model.setPublishTime(jsonObject.getString("publishTime"));
        model.setReportTitle(jsonObject.getString("title"));
        model.setReportPath(jsonObject.getString("attachPath"));
        final String reportTitle = model.getReportTitle();
        model.setReportYear(matchReportYear(reportTitle));
        final D060201Extractor.ReportCategory matchedReportCategory = matchReportCategory(reportTitle);
        if (Objects.nonNull(matchedReportCategory)) {
            model.setReportCategory(matchedReportCategory.name());
        }
        return model;
    }

    /**
     * 匹配报告年份
     *
     * @param reportTitle
     * @return
     */
    private String matchReportYear(final String reportTitle) {
        Matcher matcher = REPORT_YEAR_PATTERN.matcher(reportTitle);
        if (matcher.find()) {
            return matcher.group().substring(0, 4);
        }
        return null;
    }

    /**
     * 匹配报告类型
     *
     * @param reportTitle
     * @return
     */
    private D060201Extractor.ReportCategory matchReportCategory(final String reportTitle) {
        return EnumUtils.getEnumList(D060201Extractor.ReportCategory.class).stream().filter(item -> reportTitle.contains(item.getDescription())).findFirst().orElse(null);
    }
}
